package ua.yandex.skipasssystem.skipass;

import ua.yandex.collections.MyArrayList;
import ua.yandex.skipasssystem.skipass.enums.NumberOfDays;
import ua.yandex.skipasssystem.skipass.validators.NumberOfDaysSkiPassValidator;
import ua.yandex.skipasssystem.skipass.validators.Validator;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.EnumMap;

public class NumberOfDaysSkiPass extends WithDaysOfWorkSkiPass
        implements NumberOfDaysInterface {
    private int numberOfDays;
    private MyArrayList daysOfUsage;
    private int numberOfDaysOfUsage;

    NumberOfDaysSkiPass(long id,
                        LocalDate fromDate,
                        LocalDate toDate,
                        NumberOfDays daysNumber,
                        EnumMap<DayOfWeek, Boolean> daysOfWork) {
        super(id, fromDate, toDate, daysOfWork);
        this.numberOfDays = daysNumber.getValue();
        daysOfUsage = new MyArrayList(numberOfDays);
        numberOfDaysOfUsage = 0;
    }

    public boolean ifCanAddDay(LocalDate date) {
        if (isEnoughSpace() && !isDayExist(date)) {
            daysOfUsage.add(date);
            numberOfDaysOfUsage++;
            return true;
        } else {
            return false;
        }
    }

    private boolean isEnoughSpace() {
        return numberOfDays > numberOfDaysOfUsage;
    }

    private boolean isDayExist(LocalDate date) {
        for (int i = 0; i < numberOfDaysOfUsage; i++) {
            if ((daysOfUsage.get(i)).equals(date)) {
                return true;
            }
        }
        return false;
    }

    int getNumberOfDays() {
        return numberOfDays;
    }

    @Override
    public Validator getValidator() {
        return NumberOfDaysSkiPassValidator.getValidator();
    }
}
